#include "doge-hash.h"


hash_ret_t doge_hashtable_init(hashtable_t *h, int size)
{
    int i;

    h->count = size;
    h->elem = (int *)malloc(size*sizeof(int));
    if (h->elem == NULL) return D_HASH_UNSUCCESS;

    for (i = 0; i < size; i++)
        h->elem[i] = D_HASH_NULLKEY;

    return D_HASH_SUCCESS;
}

/**
 * @brief: 计算hash地址 
 * @param: 
 * @retval:  
 * @note:  
 */
static int hash(hashtable_t *h, int key)
{
    return key % h->count; //这里使用的是除留余数法
}


hash_ret_t doge_hashtable_insert(hashtable_t *h, int key)
{
    int addr = hash(h, key); //根据关键字求取地址
    while (h->elem[addr] != D_HASH_NULLKEY) //如果不为空，有冲突出现
        addr = (addr + 1) % h->count; //这里使用开放定址法的线性探测

    h->elem[addr] = key; //直到有空位后插入
    return D_HASH_SUCCESS;
}


hash_ret_t doge_hashtable_search(hashtable_t *h, int key, int *addr)
{
    *addr = hash(h, key);  //求取哈希地址
    while (h->elem[*addr] != key) //如果不为空，则存在冲突
    {
        *addr = (*addr + 1) % h->count;  //开发定址法的线性探测

        if ((h->elem[*addr] == D_HASH_NULLKEY) || (*addr == hash(h, key)))
            return D_HASH_UNSUCCESS; //关键字不存在
    }
    return D_HASH_SUCCESS; //查找成功返回
}

hash_ret_t doge_hashtable_delete(hashtable_t *h, int key)
{
    int addr;

    hash_ret_t ret = doge_hashtable_search(h, key, &addr)
    if (ret == D_HASH_UNSUCCESS) return ret;

    h->elem[]
}

